package com.ruoyi.cms.blog.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.ruoyi.cms.blog.domain.dto.CmsAdvertisedDTO;
import com.ruoyi.cms.blog.domain.vo.CmsAdvertisedVO;
import com.ruoyi.cms.blog.service.ICmsAdvertisedService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.security.cert.X509Certificate;
import java.util.List;

@RequiredArgsConstructor
@RestController
@RequestMapping("/cms/advertised")
public class CmsAdvertisedController extends BaseController {

    private final ICmsAdvertisedService iCmsAdvertisedService;

    /**
     * 分页
     *
     * @param dto 入参
     * @return 结果
     */

    @GetMapping("/list")
    public TableDataInfo list(CmsAdvertisedDTO dto) {
        startPage();
        List<CmsAdvertisedVO> list = iCmsAdvertisedService.list(dto);
        return getDataTable(list);
    }

    /**
     * 仅显示当前用户发布的广告列表
     *
     * @param dto
     * @return
     */
    @GetMapping("/list2")
    public TableDataInfo list2(CmsAdvertisedDTO dto) {
        Long userId = SecurityUtils.getUserId();
        dto.setCreateBy(userId.toString());
        startPage();
        List<CmsAdvertisedVO> list = iCmsAdvertisedService.list(dto);
        return getDataTable(list);
    }

    /**
     * 置顶列表
     * @param dto 入参
     * @return 结果
     */
    @GetMapping("/top")
    public AjaxResult topList(CmsAdvertisedDTO dto) {
        return iCmsAdvertisedService.topList(dto);
    }

    @PostMapping("/insert")
    public AjaxResult insert(@RequestBody @Validated CmsAdvertisedDTO dto) {
        return iCmsAdvertisedService.insert(dto);
    }

    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable Long id) {
        return iCmsAdvertisedService.getById(id);
    }

    @PostMapping("/update")
    public AjaxResult update(@RequestBody @Validated CmsAdvertisedDTO dto) {
        return iCmsAdvertisedService.update(dto);
    }

    @GetMapping("/update/{id}")
    public AjaxResult getUpdateById(@PathVariable Long id) {
        return iCmsAdvertisedService.getUpdateId(id);
    }

    @PostMapping("/updateTime/{id}")
    public AjaxResult updateTime(@PathVariable Long id) {
        return iCmsAdvertisedService.updateTime(id);
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable Long id) {
        return iCmsAdvertisedService.delete(id);
    }

    @GetMapping("/test")
    public AjaxResult test(@RequestParam("zhou") String zhou) {
        try {
            return iCmsAdvertisedService.test(zhou);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    @GetMapping("/test2")
    public AjaxResult test2() {
        return iCmsAdvertisedService.test2();
    }

}
